package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.scd.bean.Comment;
import com.kgc.scd.form.CommentForm;
import com.kgc.scd.form.ReviewForm;
import com.kgc.scd.mapper.CommentMapper;
import com.kgc.scd.service.CommentService;
import com.kgc.scd.service.OrderService;
import com.kgc.scd.util.PageSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * Created On : 2022/11/14.
 * <p>
 * Author : #jiangguoqiang
 * <p>
 * Description: CommentServiceImpl
 */
@Service
@Slf4j
public class CommentServiceImpl implements CommentService {

    @Autowired(required = false)
    private CommentMapper commentMapper;
    @Autowired(required = false)
    private OrderService orderService;

    /**
     * @author : Administrator
     * @date   : 2022/11/14
     * @param  : [userId, pageNo, pageSize]
     * @return : com.kgc.scd.util.PageSupport<com.kgc.scd.bean.Comment>
     * @description : 根据用户id带分页查询评论
     */
    @Override
    public PageSupport<Comment> getCommentListAndPage(String goodsId, Integer pageNo, Integer pageSize) {


        // 创建统一返回分页对象
        PageSupport<Comment> pageSupport = new PageSupport<>();

        LambdaQueryWrapper<Comment> wrapper = Wrappers.lambdaQuery();
        if (!Objects.isNull(goodsId)){
            wrapper.eq(Comment::getGoodsId,goodsId);
        }

        //分页
        Page<Comment> evaPage = new Page<>(pageNo,pageSize);
        Page<Comment> evaIPage = commentMapper.selectPage(evaPage, wrapper);


        // 添加分页的页码数据
        pageSupport.setPageNo(pageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalPage((int) evaIPage.getPages());
        pageSupport.setTotalCount((int) evaIPage.getTotal());

        pageSupport.setPageData(evaIPage.getRecords());
        log.info("--- 总条数：{} ---",evaIPage.getTotal());
        log.info("--- 总页数：{} ---",evaIPage.getPages());



        return pageSupport;
    }


    /**
     * @author : Administrator
     * @date   : 2022/11/15
     * @param  : [comment]
     * @return : boolean
     * @description : 增加评论
     */
    @Override
    @Transactional
    public boolean addCommentList(CommentForm commentForm) {
        Comment comment = this.getCommentByForm(commentForm);
        if (orderService.selectOrderState(comment.getOrderId()) && commentMapper.insert(comment) > 0){
            this.orderService.updateOrderState(comment.getOrderId(),7);
        }
        return false;
    }
    /**
     * @param reviewForm
     * @return : boolean
     * @author : Administrator
     * @date : 2022/11/15
     * @description : 追加评论
     */
    @Override
    @Transactional
    public boolean updateReviewList(ReviewForm reviewForm) {
        Comment comment = this.getReviewByFrom(reviewForm);
        if (orderService.selectOrdersState(comment.getOrderId()) && commentMapper.updateById(comment)>0){
            this.orderService.updateOrderState(comment.getOrderId(),8);
        }
        return commentMapper.updateById(comment)>0;
    }

    private Comment getCommentByForm(CommentForm commentForm){
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentForm,comment);
        comment.setCreationTimeContent(new Date()); // 评论时间
        comment.setUserId(1); // 用户为当前登录用户ID
        comment.setOrderId(comment.getOrderId()); // 用户为当前登录用户ID
        return comment;
    }

    public Comment getReviewByFrom(ReviewForm reviewForm){
        Comment comment = new Comment();
        BeanUtils.copyProperties(reviewForm,comment);
        comment.setCreationTimeReview(new Date());//追加评论时间
        return comment;
    }
}
